/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com;

import com.example.app.R;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.Toolbar.OnMenuItemClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年7月14日 下午2:10:44
 * @Title <B>todo</B>
 * @Description <pre>todo</pre>
 */

public abstract class BaseActivity extends /*AppCompatActivity*/ActionBarActivity {
	LinearLayout layout;
	Toolbar toolbar;
	/* (non-Javadoc)
	 * @see android.support.v7.app.AppCompatActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//一种DelayLoad的实现和原理
		/*getWindow().getDecorView().post(new Runnable() {
			@Override
			public void run() {
			myHandler.post(mLoadingRunnable);
			}
			});*/
		
		layout=new LinearLayout(this);
		layout.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
		layout.setOrientation(LinearLayout.VERTICAL);
		toolbar=(Toolbar)View.inflate(layout.getContext(), R.layout.toolbar, null);
		layout.addView(toolbar);
		setSupportActionBar(toolbar);
		toolbar.setBackgroundColor(Color.GREEN);
		//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		//toolbar.setTitle(R.string.app_name);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		toolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem paramMenuItem) {
				System.out.println("onMenuItemClick");
				return onOptionsItemSelected(paramMenuItem);
			}
		});
	}
	
	public void setTitle(CharSequence title) {
		toolbar.setTitle(R.string.app_name);	
	}
	
	/* (non-Javadoc)
	 * @see android.support.v7.app.AppCompatActivity#setContentView(int)
	 */
	@Override
	public void setContentView(int layoutResID) {
		if(layout!=null){
			View.inflate(layout.getContext(), layoutResID, layout);	
			setContentView(layout);
		}else{
			//getDelegate().setContentView(layoutResID);
			super.setContentView(layoutResID);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		System.out.println("onOptionsItemSelected");
		if(item.getItemId()==android.R.id.home){
			onBackPressed();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

}
